﻿Imports MyDLL

Public Class Form16

    Dim Nav As New MyDLL.RecordNav

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim C As String = TextBox7.Text
        Dim Author As String = TextBox1.Text
        Dim Title As String = TextBox10.Text

        If CheckBox1.Checked Then
            Dim authorxml As XElement = _
            <Author><%= Author %></Author>

            Dim titlexml As XElement = _
            <Title><%= Title %></Title>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & "<Document>" & vbCr _
                                      & "<Type>Documents</Type>" & vbCr & authorxml.ToString & vbCr & titlexml.ToString & vbCr
        End If

        If CheckBox2.Checked Then
            Dim authorxaml As XElement = _
            <Paragraph FontFamily="Arial" FontSize="20pt">
                Author: <%= Author %>
            </Paragraph>

            Dim titlexaml As XElement = _
             <Paragraph FontFamily="Arial" FontSize="20pt">
                Title: <%= Title %>
             </Paragraph>

            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & _
            "<FlowDocument xmlns=" & C & "http://schemas.microsoft.com/winfx/2006/xaml/presentation" & C & ">" & vbCr & titlexaml.ToString & vbCr & authorxaml.ToString & vbCr
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        WriteData()
    End Sub

    Private Sub WriteData()
        Dim ID As Integer = TextBox6.Text
        Dim Heading As String = TextBox2.Text
        Dim ShowHead As String = CheckBox4.CheckState.ToString
        Dim Content As String = RichTextBox1.Text

        'Addenda TXT and XML
        Dim XMLfolder As String = TextBox3.Text
        Dim XMLname As String = TextBox4.Text
        Dim XMLinclude As String = CheckBox5.CheckState.ToString
        Dim TXTfolder As String = TextBox9.Text
        Dim TXTname As String = TextBox5.Text
        Dim TXTinclude As String = CheckBox6.CheckState.ToString

        If CheckBox3.Checked Then
            ID = ID + 1
            TextBox6.Text = ID
        End If

        If CheckBox1.Checked Then
            Dim Paragraphxml As XElement = _
            <Paragraph>
                <Heading id=<%= ID %>><%= Heading %></Heading>
                <ShowHead id=<%= ID %>><%= ShowHead %></ShowHead>
                <Content id=<%= ID %>><%= Content %></Content>
                <XMLfolder id=<%= ID %>><%= XMLfolder %></XMLfolder>
                <XMLname id=<%= ID %>><%= XMLname %></XMLname>
                <XMLinclude id=<%= ID %>><%= XMLinclude %></XMLinclude>
                <TXTfolder id=<%= ID %>><%= TXTfolder %></TXTfolder>
                <TXTname id=<%= ID %>><%= TXTname %></TXTname>
                <TXTinclude id=<%= ID %>><%= TXTinclude %></TXTinclude>
            </Paragraph>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Paragraphxml.ToString & vbCr

        End If

        If CheckBox2.Checked Then

            Dim Paragraphxaml As XElement = _
            <List FontFamily="Arial" FontSize="11pt">
                <ListItem><Paragraph>ID:<%= ID %></Paragraph></ListItem>
                <ListItem><Paragraph>Heading:<%= Heading %></Paragraph></ListItem>
                <ListItem><Paragraph>Content:<%= Content %></Paragraph></ListItem>
                <ListItem><Paragraph>XML Addendum:<%= XMLfolder %>\<%= XMLname %>.xml</Paragraph></ListItem>
                <ListItem><Paragraph>TXT Addendum:<%= TXTfolder %>\<%= TXTname %>.txt</Paragraph></ListItem>
            </List>

            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & Paragraphxaml.ToString & vbCr
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Count As String = TextBox6.Text

        If CheckBox1.Checked Then
            Dim Counter As XElement = _
            <Counter>
                <Count><%= Count %></Count>
            </Counter>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Counter.ToString & vbCr & "</Document>"
        End If

        If CheckBox2.Checked Then
            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & "</FlowDocument>"
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        LoadData()
    End Sub
    Private Sub LoadData()
        Try
            Me.Text = "Document Database" & " - " & Form1.TextBox3.Text

            Dim ID As String = TextBox6.Text

            Dim Document As XElement = XElement.Load(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

            Dim Heading As XElement = _
                <Root>
                    <%= From el In Document.<Paragraph>.<Heading> _
                        Where el.Attribute("id") = ID _
                        Select el %>
                </Root>
            TextBox2.Text = Heading

            Dim ShowHead As XElement = _
                <Root>
                    <%= From el In Document.<Paragraph>.<ShowHead> _
                        Where el.Attribute("id") = ID _
                        Select el %>
                </Root>
            Dim ShowHeadt As String = ShowHead
            If ShowHeadt = "Checked" Then
                CheckBox4.CheckState = CheckState.Checked
            ElseIf ShowHeadt = "Unchecked" Then
                CheckBox4.CheckState = CheckState.Unchecked
            Else
                CheckBox4.CheckState = CheckState.Unchecked
            End If

            Dim Content As XElement = _
               <Root>
                   <%= From el In Document.<Paragraph>.<Content> _
                       Where el.Attribute("id") = ID _
                       Select el %>
               </Root>
            RichTextBox1.Text = Content

            Dim XMLfolder As XElement = _
               <Root>
                   <%= From el In Document.<Paragraph>.<XMLfolder> _
                       Where el.Attribute("id") = ID _
                       Select el %>
               </Root>
            TextBox3.Text = XMLfolder

            Dim XMLname As XElement = _
              <Root>
                  <%= From el In Document.<Paragraph>.<XMLname> _
                      Where el.Attribute("id") = ID _
                      Select el %>
              </Root>
            TextBox4.Text = XMLname

            Dim XMLinclude As XElement = _
              <Root>
                  <%= From el In Document.<Paragraph>.<XMLinclude> _
                      Where el.Attribute("id") = ID _
                      Select el %>
              </Root>
            Dim XMLincludet = XMLinclude
            If XMLincludet = "Checked" Then
                CheckBox5.CheckState = CheckState.Checked
            ElseIf XMLincludet = "Unchecked" Then
                CheckBox5.CheckState = CheckState.Unchecked
            Else
                CheckBox5.CheckState = CheckState.Unchecked
            End If

            Dim TXTfolder As XElement = _
               <Root>
                   <%= From el In Document.<Paragraph>.<TXTfolder> _
                       Where el.Attribute("id") = ID _
                       Select el %>
               </Root>
            TextBox9.Text = TXTfolder

            Dim TXTname As XElement = _
              <Root>
                  <%= From el In Document.<Paragraph>.<TXTname> _
                      Where el.Attribute("id") = ID _
                      Select el %>
              </Root>
            TextBox5.Text = TXTname

            Dim TXTinclude As XElement = _
              <Root>
                  <%= From el In Document.<Paragraph>.<TXTinclude> _
                      Where el.Attribute("id") = ID _
                      Select el %>
              </Root>
            Dim TXTincludet = TXTinclude
            If TXTincludet = "Checked" Then
                CheckBox6.CheckState = CheckState.Checked
            ElseIf TXTincludet = "Unchecked" Then
                CheckBox6.CheckState = CheckState.Unchecked
            Else
                CheckBox6.CheckState = CheckState.Unchecked
            End If

            Dim Author As XElement = _
               <Root>
                   <%= From el In Document.<Author> _
                       Select el %>
               </Root>
            TextBox1.Text = Author

            Dim Title As XElement = _
               <Root>
                   <%= From el In Document.<Title> _
                       Select el %>
               </Root>
            TextBox10.Text = Title


            Dim Count As XElement = _
        <Root>
            <%= From el In Document.<Counter>.<Count> _
                Select el %>
        </Root>

            Dim Counter As String = Count

            Label7.Text = "There are " & Counter & " record(s) in the XML file"
        Catch ex As Exception
            MsgBox("No file has been loaded.")

        End Try

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Try
            Do Until TextBox6.Text = TextBox8.Text
                CheckBox3.CheckState = CheckState.Unchecked
                Dim ID As Integer = TextBox6.Text
                ID = ID + 1
                TextBox6.Text = ID
                LoadData()
                WriteData()
            Loop
        Catch ex As Exception
            MsgBox("Choose a proper range.")
        End Try
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Nav.GoToFirst(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Nav.PrevRec(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        Nav.NextRec(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        Nav.GoToLast(TextBox6, Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

        LoadData()
    End Sub
End Class